package com.example.service.impl;

import com.example.entity.farm.PlayerFarm;
import com.example.entity.farm.dto.StockPlayerFarmDTO;
import com.example.entity.item.Items;
import com.example.mapper.UserMapper;
import com.example.mapper.dao.FarmDao;
import com.example.service.FarmService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

@Service
public class FarmServiceImpl implements FarmService {

    @Resource
    private FarmDao farmDao;

    @Resource
    private UserMapper userMapper;

    @Override
    public String plant(String uid, Integer fertilizer) {
        PlayerFarm playerFarm = farmDao.selectPlayerFarmByUid(uid);

        if(fertilizer >0)
        {
            playerFarm.setProgress(playerFarm.getProgress()+2*fertilizer);
            farmDao.updatePlayerFarmProgress(uid,playerFarm.getProgress());

            playerFarm.setFertilizer(playerFarm.getFertilizer()-fertilizer);
            farmDao.updatePlayerFarmFertilizer(uid,playerFarm.getFertilizer());

            return "施肥成功";
        }
        else{
            farmDao.updatePlayerFarmProgress(uid,playerFarm.getProgress()+1);
            farmDao.water(uid);
            return "浇水成功";
        }
    }

    @Override
    public StockPlayerFarmDTO desc(String uid) {
        PlayerFarm playerFarm= farmDao.desc(uid);
        StockPlayerFarmDTO stockPlayerFarmDTO = new StockPlayerFarmDTO();

        stockPlayerFarmDTO.setUid(playerFarm.getUid());
        stockPlayerFarmDTO.setProgress(playerFarm.getProgress());
        stockPlayerFarmDTO.setFertilizer(playerFarm.getFertilizer());
        stockPlayerFarmDTO.setWater(playerFarm.getWater());


        Items item=userMapper.selectItemById(playerFarm.getItemID());
        stockPlayerFarmDTO.setItemID(item.getItemID());
        stockPlayerFarmDTO.setItemName(item.getItemName());
        stockPlayerFarmDTO.setItemImg(item.getItemImg());
        stockPlayerFarmDTO.setItemDesc(item.getItemDesc());

        return stockPlayerFarmDTO;
    }

    @Override
    public String goFarm(String uid,String itemId) {
        if(farmDao.goFarm(uid,itemId)==1){
            return "yes";
        }
        return "no";
    }
}
